package org.cqupt.content.counter.utils;

import org.cqupt.content.counter.entity.ArticleEntity;

public class CountWord {
	/**
	 * 根据文章内容统计符号数
	 * @param arEntity
	 */
	public static void count(ArticleEntity arEntity) {
		String content = arEntity.getContent();
		int chineseWord = 0;
		int englishWord = 0;
		int marksCount = 0;
		int total = 0;
		for (int i = 0; i < content.length(); i++) {
			char chara = content.charAt(i);
			if ((chara >= 'A' && chara <= 'Z') || (chara >= 'a' && chara <= 'z')) {
				englishWord++;
			} else if (isChineseWord(chara)) {
				chineseWord++;
			} else if (chara == ' '){
				continue;
			} else {
				marksCount++;
			}
			total++;
		}
		arEntity.setChineseWord(chineseWord);
		arEntity.setEnglishWord(englishWord);
		arEntity.setMarksCount(marksCount);
		arEntity.setWordCount(total);
	}

	/***
	 * 判断字符是否为中文
	 * 
	 * @param chara
	 *            需要判断的字符
	 * @return 中文返回true，非中文返回false
	 */
	private static boolean isChineseWord(char chara) {
		Character.UnicodeBlock ub = Character.UnicodeBlock.of(chara);
		if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS) {
			return true;
		}
		return false;
	}
}
